package com.lmqb.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lmqb.bean.JCheckArtificial;
import com.lmqb.bean.LmAccount;
import com.lmqb.bean.LmCredit;
import com.lmqb.bean.LmCreditPromote;
import com.lmqb.bean.LmLoan;
import com.lmqb.bean.LmRepay;
import com.lmqb.bean.LmUser;
import com.lmqb.bean.LmUserBank;
import com.lmqb.bean.LmUserInfo;
import com.lmqb.bean.LmUserLatefee;
import com.lmqb.bean.SysBank;
import com.lmqb.bean.SysUser;
import com.lmqb.constant.Status;
import com.lmqb.dao.JCheckArtificialMapper;
import com.lmqb.dao.LmCreditMapper;
import com.lmqb.dao.LmCreditPromoteMapper;
import com.lmqb.dao.LmRepayMapper;
import com.lmqb.dao.LmUserBankMapper;
import com.lmqb.dao.LmUserInfoMapper;
import com.lmqb.dao.LmUserLatefeeMapper;
import com.lmqb.dao.LmUserMapper;
import com.lmqb.dao.LmLoanMapper;
import com.lmqb.dao.SysBankMapper;
import com.lmqb.dao.SysUserMapper;
import com.lmqb.entry.CreditCheck;
import com.lmqb.entry.DaKuan;
import com.lmqb.entry.UBaseInfo;
import com.lmqb.page.CreditCheckPager;
import com.lmqb.page.CreditPager;
import com.lmqb.page.CreditPromotePager;
import com.lmqb.page.HitsPager;
import com.lmqb.page.LatefeePager;
import com.lmqb.page.LmUserPager;
import com.lmqb.page.LoanPager;
import com.lmqb.page.PageTool;
import com.lmqb.page.RepayPager;
import com.lmqb.service.CreditService;
import com.lmqb.util.DesUtils;
import com.lmqb.util.StrUtil;
@Service("creditService")
public class CreditServiceImpl implements CreditService{

	@Autowired
	private LmCreditPromoteMapper creditPromoteDao;
	
	@Autowired
	private LmCreditMapper creditDao;
	
	@Autowired
	private LmUserLatefeeMapper latefeeDao;
	
	@Autowired
	private LmUserMapper lmUserDao;
	
	@Autowired
	private LmLoanMapper loanDao;
	
	@Autowired
	private LmRepayMapper repayDao;
	
	@Autowired
	private JCheckArtificialMapper checkDao;
	
	@Autowired
	private LmUserInfoMapper userInfoDao;
	
	@Autowired
	private LmUserBankMapper userBankDao;
	
	@Autowired
	private SysUserMapper sysUserDao;
	
	@Autowired
	private LmUserInfoMapper lmUserInfoMapper;
	
	
	/**
	 * @show  借款人信用额度提升申请表（提额申请）
	 *
	 * @param CreditPromotePager
	 * 
	 * @return CreditPromotePager
	 * */
	public CreditPromotePager selectAccountPager(CreditPromotePager pagerModel) {
		List<LmCreditPromote> list=creditPromoteDao.selectAll(pagerModel);
//		List<LmUserInfo> userList=userInfoDao.selectAll();
		for(int i=0;i<list.size();i++){
			list.get(i).setApplyTimeD(StrUtil.intToTime(list.get(i).getApplyTime()));
			list.get(i).setLastModifyTimeD(StrUtil.intToTime(list.get(i).getLastModifyTime()));
//			for(int j=0;j<userList.size();j++){
//				if(list.get(i).getUserId()==userList.get(j).getUserId()){
//					list.get(i).setUserName(userList.get(j).getUserName());
//					break;
//				}
//			}
		}
		pagerModel.setList(list);
		int count = creditPromoteDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 * @show  提额用户
	 *
	 * @param CreditPager
	 * 
	 * @return CreditPager
	 * */	
	public CreditPager selectCreditPager(CreditPager pagerModel) {
		List<LmCredit> list=creditDao.selectAll(pagerModel);
		
//		List<LmUserInfo> userList=userInfoDao.selectAll();
		List<LmCreditPromote> credits=creditPromoteDao.selectAllMax();
		for(int i=0;i<list.size();i++){
			
			/*LmUserInfo selectByUserId = lmUserInfoMapper.selectByUserId(list.get(i).getUserId());
			if(selectByUserId.getCardNo()==null||selectByUserId.getCardNo()==""||selectByUserId.getCardNo()=="null"){
				list.get(i).setIDNum("");
			}else {
				list.get(i).setIDNum(DesUtils.decrypt(selectByUserId.getCardNo(), Status.card_id));
			}*/
			//System.out.println(list.get(i).getUserId()+"-->"+DesUtils.decrypt(selectByUserId.getCardNo(), Status.card_id));
//			for(int j=0;j<userList.size();j++){
//				if(list.get(i).getUserId()==userList.get(j).getUserId()){
//					list.get(i).setUserName(userList.get(j).getUserName());
//					list.get(i).setIDNum(userList.get(j).getCardNo());
//					break;
//				}
//			}
			for(int k=0;k<credits.size();k++){
				if(credits.get(k).getUserId()==list.get(i).getUserId()){
					//获取身份证信息
					//LmUserInfo lmUserInfo = lmUserInfoMapper.selectByPrimaryKey(list.get(i).getUserId());
					
					//list.get(i).setIDNum(lmUserInfo.getCardNo());
					list.get(i).setRemark(credits.get(k).getCheckRemark());
					list.get(i).setCheckTime(StrUtil.intToTime(credits.get(k).getLastModifyTime()));
					
					break;
				}
			}
		}
		pagerModel.setList(list);
		int count = creditDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 * @show 滞纳金明细
	 * 
	 * @param LatefeePager
	 * 
	 * @return LatefeePager
	 * */
	public LatefeePager selectLatefeePager(LatefeePager pagerModel) {
		List<LmUserLatefee> list=latefeeDao.selectAll(pagerModel);
		for(int i=0;i<list.size();i++){
			list.get(i).setAddTimeD(StrUtil.intToTime(list.get(i).getAddTime()));
		}
		pagerModel.setList(list);
		int count = latefeeDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 * @show 借款列表
	 * 
	 * @param LoanPager
	 * 
	 * @return LoanPager
	 * */
	public LoanPager selectLoanPager(LoanPager pagerModel) {
		List<LmLoan> list=loanDao.selectJieKuan(pagerModel);//借款列表，还没有开始终审的。
		List<LmUser> userList=lmUserDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			list.get(i).setLoanTimeD(StrUtil.intToTime(list.get(i).getLoanTime()));
			list.get(i).setPayTimeD(StrUtil.intToTime(list.get(i).getPayTime()));
			list.get(i).setRepaymentSuccessTimeD(StrUtil.intToTime(list.get(i).getRepaymentSuccessTime()));
			for(int j=0;j<userList.size();j++){
				if(list.get(i).getUserId()==userList.get(j).getUserId()){
					list.get(i).setUserName(userList.get(j).getLoanMobile());
					break;
				}
			}
		}
		pagerModel.setList(list);
		int count = loanDao.selectCountJieKuan(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 * @show 还款明细
	 * 
	 * @param RepayPager
	 * 
	 * @return RepayPager
	 * */
	 public RepayPager selectRepayPager(RepayPager pagerModel) {
		List<LmRepay> list=repayDao.selectAll(pagerModel);
//		List<LmUser> userList=lmUserDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			list.get(i).setRepayEndTimeD(StrUtil.intToTime(list.get(i).getRepayEndTime()));
			list.get(i).setRepayYesTimeD(StrUtil.intToTime(list.get(i).getRepayYesTime()));
//			for(int j=0;j<userList.size();j++){
//				if(list.get(i).getUserId()==userList.get(j).getUserId()){
//					list.get(i).setUserName(userList.get(j).getLoanMobile());
//					break;
//				}
//			}
		}
		pagerModel.setList(list);
		int count = repayDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}
	
	/**
	 * @show 放款列表
	 * 
	 * @param LoanPager
	 * 
	 * @return LoanPager
	 * 
	 * */
	public LoanPager selectAllPager(LoanPager pagerModel) {
		List<LmLoan> list=loanDao.selectAll(pagerModel);//初审、复审、终审都在这里
//		List<LmUser> userList=lmUserDao.selectAllUser();
		for(int i=0;i<list.size();i++){
			list.get(i).setLoanTimeD(StrUtil.intToTime(list.get(i).getLoanTime()));
			list.get(i).setPayTimeD(StrUtil.intToTime(list.get(i).getPayTime()));
			list.get(i).setRepaymentSuccessTimeD(StrUtil.intToTime(list.get(i).getRepaymentSuccessTime()));
//			for(int j=0;j<userList.size();j++){
//				if(list.get(i).getUserId()==userList.get(j).getUserId()){
//					list.get(i).setUserName(userList.get(j).getLoanMobile());
//					break;
//				}
//			}
		}
		pagerModel.setList(list);
		int count = loanDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}
	
	/**
	 * @show //打款
	 * 
	 * @param HitsPager
	 * 
	 * @return HitsPager
	 * 
	 * */
	public HitsPager selectDaKuan(HitsPager pagerModel) {
		List<LmLoan> list=loanDao.selectHits(pagerModel);
	//	List<LmUserInfo> userList=userInfoDao.selectAll();
		List<LmUserBank> userBanks=userBankDao.selectAllUser();
		List<DaKuan> dakuanList=new ArrayList<DaKuan>();
		for(int i=0;i<list.size();i++){
			DaKuan hits=new DaKuan();
			for(int m=0;m<userBanks.size();m++){
				if(userBanks.get(m).getBankId().intValue()==list.get(i).getBankId().intValue()){
					hits.setBankName(userBanks.get(m).getBankName());
					hits.setBankNum(DesUtils.decrypt(userBanks.get(m).getBankAccount(), Status.bank_account));
					break;
				}
			}
			hits.setIDNum(DesUtils.decrypt(list.get(i).getIDNum(), Status.card_id));
			hits.setUserName(list.get(i).getUserName());
			hits.setLoanMoney(list.get(i).getLoanMoney());
			hits.setLoanNo(list.get(i).getLoanNo());
			hits.setLoanTime(StrUtil.intToTime(list.get(i).getLoanTime()));
			hits.setPayStatus(list.get(i).getPayStatus());
			dakuanList.add(hits);
		}
		pagerModel.setList(dakuanList);
		int count = loanDao.selectCountHits(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}

	/**
	 * @show 材料审核列表  逻辑处理可能要改 7.6
	 * 
	 * @param CreditCheckPager
	 * 
	 * @return CreditCheckPager
	 * 
	 * */
	public CreditCheckPager selectAllCredit(CreditCheckPager pagerModel) {
		List<JCheckArtificial> list=checkDao.selectAllByType(pagerModel);//根据审核人员来查
		List<JCheckArtificial> checkAll=checkDao.selectAll();  //查询全部审核
		List<LmUserInfo> userInfos=userInfoDao.selectAll();  //查询所有用户信息表
		List<CreditCheck> checks=new ArrayList<CreditCheck>(); //额度审核
		List<SysUser> sysUsers=sysUserDao.selectAllUser();  //查询所有管理员
		System.out.println(checkAll.size()+"----->"+list.size());
		   for(int i=0;i<list.size();i++){
		   CreditCheck check=new CreditCheck();
			System.out.println(list.size()+"-----"+checkAll.size());
		if(list.get(i).getType()>0){//初审的管理员
				//根据初审id号找到初审id号相同、Type为1的记录的管理员id，查找管理员姓名
			
				for(int j=0;j<checkAll.size();j++){
					//如果是初审人员审核的
					if(checkAll.get(j).getCheckOneId().equals(list.get(i).getCheckOneId())&&checkAll.get(j).getType()==(byte)1 ){
						for(int k=0;k<sysUsers.size();k++){
							if(checkAll.get(j).getAdminId().equals(sysUsers.get(k).getUserId())){
								check.setAdminId1(sysUsers.get(k).getUserId());
								check.setAdminName1(sysUsers.get(k).getLoginName());
								check.setStatus1(checkAll.get(j).getStatus());
							}
						}
					}
				}
			
		
		        if(list.get(i).getType()>1){//复审管理员
					for(int j=0;j<checkAll.size();j++){
						if(checkAll.get(j).getCheckOneId().equals(list.get(i).getCheckOneId())&&checkAll.get(j).getType()==(byte)2){
							for(int k=0;k<sysUsers.size();k++){
								if(checkAll.get(j).getAdminId().equals(sysUsers.get(k).getUserId())){
									check.setAdminId2(sysUsers.get(k).getUserId());
									check.setAdminName2(sysUsers.get(k).getLoginName());
									check.setStatus2(checkAll.get(j).getStatus());
								}
							}
						}
					}
				}
		        
				if(list.get(i).getType()>2){//终审
					for(int j=0;j<checkAll.size();j++){
						if(checkAll.get(j).getCheckOneId().equals(list.get(i).getCheckOneId())&&checkAll.get(j).getType()==(byte)3){
							for(int k=0;k<sysUsers.size();k++){
								if(checkAll.get(j).getAdminId().equals(sysUsers.get(k).getUserId())){
									check.setAdminId3(sysUsers.get(k).getUserId());
									check.setAdminName3(sysUsers.get(k).getLoginName());
									check.setStatus3(checkAll.get(j).getStatus());
								}
							}
						}
					}
				}
				
		}
		
			for(int m=0;m<userInfos.size();m++){
				if(list.get(i).getUserId().equals(userInfos.get(m).getUserId())){
					check.setUserName(userInfos.get(m).getUserName());
					check.setAuthStep(userInfos.get(m).getAuthStep());
					
					}
				}
			check.setCreateTime(list.get(i).getCreateTime());
			check.setApplyTime(list.get(i).getApplyTime());
			check.setFenshu(null);
			check.setRecId(list.get(i).getRecId());
			check.setType(list.get(i).getType());
			check.setUserId(list.get(i).getUserId());
			check.setCredit(list.get(i).getCredit());
		   checks.add(check);
		}
		   
		pagerModel.setList(checks);
		int count=checkDao.selectCount(pagerModel);
		pagerModel.setTotalRow(count);
		int totalPage = PageTool.getTotalPage(count, pagerModel.getPageSize());
		pagerModel.setTotalPage(totalPage);
		return pagerModel;
	}
	/**
	 * @show  提额审核不通过
	 *
	 * @param LmCreditPromote
	 * 
	 * @return LmCreditPromote
	 * */	
	public int updateNoPassCreiet(LmCreditPromote record) {
		int creiet = creditPromoteDao.updateNoPassCreiet(record);
		return creiet;
	}
	
	
	/**
	 * @show  通过promoteId找LmCreditPromote
	 *
	 * @param Integer
	 * 
	 * @return Integer
	 * */		
	public LmCreditPromote selectByPrimaryKey(Integer promoteId) {
		LmCreditPromote lmCreditPromote = creditPromoteDao.selectByPrimaryKey(promoteId);
		return lmCreditPromote;
	}

	/**
	 * @show  提额通过后
	 *
	 * @param LmCredit
	 * 
	 * @return LmCredit
	 * */		
   public int updateIsPassCrdiet(LmCredit record) {
		int updateIsPassCrdiet = creditDao.updateIsPassCrdiet(record);
		return updateIsPassCrdiet;
	}

}
